﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using CamHelper.Engine;
using Ninject;
using Ninject.MockingKernel.RhinoMock;
using Ninject.Parameters;
using NUnit.Framework;
using Rhino.Mocks;
using SystemWrapper;
using SystemWrapper.IO;

namespace CamHelper.Tests.Engine
{
    [TestFixture]
    public class TopSolidFileTest
    {

        #region Empty

        [Test]
        public void Constructor_PathIsNull()
        {
            var kernel = new RhinoMocksMockingKernel();
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", Arg<String>.Is.Null));

            TestEmpty(file);
        }

        [Test]
        public void Constructor_PathIsEmpty()
        {
            var kernel = new RhinoMocksMockingKernel();
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", Arg<String>.Is.Equal(String.Empty)));

            TestEmpty(file);
        }

        private static void TestEmpty(TopSolidFile file)
        {
            Assert.That(file.Directory, Is.EqualTo(String.Empty));
            Assert.That(file.FileType, Is.EqualTo(TopSolidFileType.Empty));
            Assert.That(file.FullName, Is.EqualTo(String.Empty));
            Assert.That(file.IsDocument, Is.False);
            Assert.That(file.IsEmpty, Is.True);
            Assert.That(file.IsSpezial, Is.False);
            Assert.That(file.IsStandardComponent, Is.False);
            Assert.That(file.Name, Is.EqualTo(String.Empty));
            Assert.That(file.RscExists, Is.False);
            Assert.That(file.LastSave.DateTimeInstance, Is.EqualTo(DateTime.MinValue));
        }

        #endregion

        [Test]
        public void Constructor_TopFile()
        {
            var path = @"c:\temp\test.top";
            var time = new DateTimeWrap(2000, 01, 01, 12, 30, 20);

            var fileMock = MockRepository.GenerateMock<IFileWrap>();
            fileMock.Expect(x => x.Exists(Arg<String>.Is.Equal(path + ".rsc"))).Return(false);
            fileMock.Expect(x => x.GetLastWriteTime(Arg<String>.Is.Equal(path))).Return(time);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IFileWrap>().ToConstant(fileMock);
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", path));

            Assert.That(file.Directory, Is.EqualTo(@"c:\temp"));
            Assert.That(file.FileType, Is.EqualTo(TopSolidFileType.Top));
            Assert.That(file.FullName, Is.EqualTo(@"c:\temp\test.top"));
            Assert.That(file.IsDocument, Is.False);
            Assert.That(file.IsEmpty, Is.False);
            Assert.That(file.IsSpezial, Is.False);
            Assert.That(file.IsStandardComponent, Is.False);
            Assert.That(file.Name, Is.EqualTo(@"test.top"));
            Assert.That(file.RscExists, Is.False);
            Assert.That(file.LastSave, Is.EqualTo(time));
        }

        [Test]
        public void Constructor_TopFileRsc()
        {
            var path = @"c:\temp\test.top";
            var time = new DateTimeWrap(2000, 01, 01, 12, 30, 20);

            var fileMock = MockRepository.GenerateMock<IFileWrap>();
            fileMock.Expect(x => x.Exists(Arg<String>.Is.Equal(path + ".rsc"))).Return(true);
            fileMock.Expect(x => x.GetLastWriteTime(Arg<String>.Is.Equal(path))).Return(time);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IFileWrap>().ToConstant(fileMock);
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", path));

            Assert.That(file.Directory, Is.EqualTo(@"c:\temp"));
            Assert.That(file.FileType, Is.EqualTo(TopSolidFileType.Top));
            Assert.That(file.FullName, Is.EqualTo(@"c:\temp\test.top"));
            Assert.That(file.IsDocument, Is.False);
            Assert.That(file.IsEmpty, Is.False);
            Assert.That(file.IsSpezial, Is.False);
            Assert.That(file.IsStandardComponent, Is.False);
            Assert.That(file.Name, Is.EqualTo(@"test.top"));
            Assert.That(file.RscExists, Is.True);
            Assert.That(file.LastSave, Is.EqualTo(time));
        }

        [Test]
        public void Constructor_CamFile()
        {
            var path = @"c:\temp\test.cam";
            var time = new DateTimeWrap(2000, 01, 01, 12, 30, 20);

            var fileMock = MockRepository.GenerateMock<IFileWrap>();
            fileMock.Expect(x => x.Exists(Arg<String>.Is.Equal(path + ".rsc"))).Return(false);
            fileMock.Expect(x => x.GetLastWriteTime(Arg<String>.Is.Equal(path))).Return(time);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IFileWrap>().ToConstant(fileMock);
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", path));

            Assert.That(file.Directory, Is.EqualTo(@"c:\temp"));
            Assert.That(file.FileType, Is.EqualTo(TopSolidFileType.Cam));
            Assert.That(file.FullName, Is.EqualTo(@"c:\temp\test.cam"));
            Assert.That(file.IsDocument, Is.False);
            Assert.That(file.IsEmpty, Is.False);
            Assert.That(file.IsSpezial, Is.False);
            Assert.That(file.IsStandardComponent, Is.False);
            Assert.That(file.Name, Is.EqualTo(@"test.cam"));
            Assert.That(file.RscExists, Is.False);
            Assert.That(file.LastSave, Is.EqualTo(time));
        }

        [Test]
        public void Constructor_StandardComponent()
        {
            var path = @"$STD=Company\Vices\Categorie#V=CustomType#I=1#R=NR.top";

            var kernel = new RhinoMocksMockingKernel();
            var file = kernel.Get<TopSolidFile>(new ConstructorArgument("path", path));

            Assert.That(file.Directory, Is.EqualTo(@"$STD=Company\Vices"));
            Assert.That(file.FileType, Is.EqualTo(TopSolidFileType.Top));
            Assert.That(file.FullName, Is.EqualTo(@"$STD=Company\Vices\Categorie#V=CustomType#I=1#R=NR.top"));
            Assert.That(file.IsDocument, Is.False);
            Assert.That(file.IsEmpty, Is.False);
            Assert.That(file.IsSpezial, Is.True);
            Assert.That(file.IsStandardComponent, Is.True);
            Assert.That(file.Name, Is.EqualTo(@"Categorie#V=CustomType#I=1#R=NR.top"));
            Assert.That(file.RscExists, Is.False);
            Assert.That(file.LastSave.DateTimeInstance, Is.EqualTo(DateTime.MinValue));
        }

    }
}
